package Thread;

public class ThreadDemo15 {
    static class MyRunnable1 implements Runnable{
        Object locker1=null;
        Object locker2=null;
        public MyRunnable1(Object locker1, Object locker2) {
            this.locker1 = locker1;
            this.locker2 = locker2;
        }
        @Override
        public void run() {
            for (int i = 0; i <10 ; i++) {
                System.out.print("A");
                synchronized (locker2){
                    locker2.notify();
                }
                synchronized (locker1){
                    try {
                        locker1.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }

    }
    static class MyRunnable2 implements Runnable{
        Object locker1=null;
        Object locker2=null;

        public MyRunnable2(Object locker1, Object locker2) {
            this.locker1 = locker1;
            this.locker2 = locker2;
        }

        @Override
        public void run() {
            for (int i = 0; i <10 ; i++) {
                synchronized (locker1){
                    try {
                        locker1.wait();
                        System.out.print("B");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                synchronized (locker2){
                    locker2.notify();
                }
            }
        }
    }
    static class MyRunnable3 implements Runnable{
        Object locker1=null;
        Object locker2=null;
        public MyRunnable3(Object locker1, Object locker2) {
            this.locker1 = locker1;
            this.locker2 = locker2;
        }

        @Override
        public void run() {
            for (int i = 0; i <10 ; i++) {
                synchronized (locker1){
                    try {
                        locker1.wait();
                        System.out.print("C");
                        System.out.println();

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                synchronized (locker2){
                    locker2.notify();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Object object1=new Object();
        Object object2=new Object();
        Object object3=new Object();
        Thread t1=new Thread(new MyRunnable1(object1,object2));
        Thread t2=new Thread(new MyRunnable2(object2,object3));
        Thread t3=new Thread(new MyRunnable3(object3,object1));
        t2.start();
        t3.start();
        t1.start();
    }
}

